package com.lagou.zuoye.servlet;

import com.lagou.zuoye.bean.Student;
import com.lagou.zuoye.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AlterServlet",urlPatterns = "/alter")
public class AlterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //获取浏览器发送来的数据
        String name = request.getParameter("name");
        String alterName = request.getParameter("alterName");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String notes = request.getParameter("notes");

        StudentService studentService = new StudentService();
        Student student = new Student(null, alterName, sex, birthday, email, notes);
        int i = studentService.StudentAlterService(student,name);
        if(0==i){
            System.out.println("修改失败，请重新输入正确的学员信息");
            request.setAttribute("alterError","删除失败，输入的学员姓名不正确！");
            //实现服务器跳转，共享request和response    对象     使用转发的操作
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("list.jsp");
            requestDispatcher.forward(request,response);
        }else{
            System.out.println("修改成功");
            request.getSession().setAttribute("student",student);
            response.sendRedirect("alter.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
